package com.cykj.service;

import com.cykj.dto.ResponseDTO;
import com.cykj.mapper.OrderMapper;
import com.cykj.pojo2.CyOrder;
import com.cykj.vo.OrderVo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private OrderMapper orderMapper;
@Autowired
private RedisTemplate redisTemplate;
    @Override
    public ResponseDTO addOrder(int userId, int goodId, int num) {
        System.out.println(userId);
        System.out.println(goodId);
        System.out.println(num);
       int stock=Integer.parseInt(String.valueOf(redisTemplate.opsForValue().get("stock:"+goodId)));
       if(num<=stock){
           redisTemplate.opsForValue().decrement("stock:"+goodId,num);
           OrderVo vo=new OrderVo();
           vo.setNum(num);
           vo.setUserId(userId);
           vo.setGoodId(goodId);

           rocketMQTemplate.convertAndSend("create-order",vo);
           return ResponseDTO.success("下单成功");
//           CyOrder cyOrder = new CyOrder();
//           cyOrder.setUserId(userId);
//           cyOrder.setGoodId(goodId);
//           cyOrder.setBuyCount(num);
//           int i=orderMapper.addOrder(cyOrder);
//           if(i>0){
//
//           }
//           return ResponseDTO.error("下单失败");
       }
       else{
           return ResponseDTO.error("库存不足");
       }
    }

}
